#ifndef DAP_USB_HANDLE_H
#define DAP_USB_HANDLE_H

#include "DAP_config.h"
#include "dap_main.h"

/***************************USB相关宏定义************************************/
#define DAP_IN_EP  0x81
#define DAP_OUT_EP 0x02
#define SWO_IN_EP 0x06

#define CDC_IN_EP  0x83
#define CDC_OUT_EP 0x04
#define CDC_INT_EP 0x85



#define MSC_IN_EP  0x86
#define MSC_OUT_EP 0x07

#define USBD_VID           0x0D28
#define USBD_PID           0x0204
#define USBD_MAX_POWER     300
#define USBD_LANGID_STRING 1033

#if (SWO_STREAM == 1)
#define CMSIS_DAP_INTERFACE_SIZE (9 + 7 + 7 + 7)

#else
#define CMSIS_DAP_INTERFACE_SIZE (9 + 7 + 7)

#endif /*SWO_STREAM*/

#ifdef CONFIG_CHERRYDAP_USE_MSC
#define CONFIG_MSC_DESCRIPTOR_LEN CDC_ACM_DESCRIPTOR_LEN
#define CONFIG_MSC_INTF_NUM       1
#define MSC_INTF_NUM              (0x02 + 1)
#else
#define CONFIG_MSC_DESCRIPTOR_LEN 0
#define CONFIG_MSC_INTF_NUM       0
#define MSC_INTF_NUM              (0x02)
#endif

#ifdef CONFIG_USB_HS
#if DAP_PACKET_SIZE != 512
#error "DAP_PACKET_SIZE must be 512 in hs"
#endif
#else
#if DAP_PACKET_SIZE != 64
#error "DAP_PACKET_SIZE must be 64 in fs"
#endif
#endif

#define USBD_WINUSB_VENDOR_CODE 0x20

#define USBD_WEBUSB_ENABLE 0
#define USBD_BULK_ENABLE   1
#define USBD_WINUSB_ENABLE 1

/* WinUSB Microsoft OS 2.0 descriptor sizes */
#define WINUSB_DESCRIPTOR_SET_HEADER_SIZE  10
#define WINUSB_FUNCTION_SUBSET_HEADER_SIZE 8
#define WINUSB_FEATURE_COMPATIBLE_ID_SIZE  20

#define FUNCTION_SUBSET_LEN                160
#define DEVICE_INTERFACE_GUIDS_FEATURE_LEN 132

#define USBD_WINUSB_DESC_SET_LEN (WINUSB_DESCRIPTOR_SET_HEADER_SIZE + USBD_WEBUSB_ENABLE * FUNCTION_SUBSET_LEN + USBD_BULK_ENABLE * FUNCTION_SUBSET_LEN)

#define USBD_NUM_DEV_CAPABILITIES (USBD_WEBUSB_ENABLE + USBD_WINUSB_ENABLE)

#define USBD_WEBUSB_DESC_LEN 24
#define USBD_WINUSB_DESC_LEN 28

#define USBD_BOS_WTOTALLENGTH (0x05 +                                      \
                               USBD_WEBUSB_DESC_LEN * USBD_WEBUSB_ENABLE + \
                               USBD_WINUSB_DESC_LEN * USBD_WINUSB_ENABLE)

#define CONFIG_UARTRX_RINGBUF_SIZE (1024)
#define CONFIG_USBRX_RINGBUF_SIZE  (1024*2)

extern __ALIGN_BEGIN const uint8_t USBD_WinUSBDescriptorSetDescriptor[];
extern __ALIGN_BEGIN const uint8_t USBD_BinaryObjectStoreDescriptor[];

/***************************USB相关宏定义  END************************************/
#define USB_UART_RB_SIZE (1024 * 3)

#define CDC_ACM_UART_NUM UART_NUM_1

#define USB_UART_CONFIG_EVENT (1<<0)  //配置事件
#define USB_UART_USB2UART_EVENT  (1<<1) //usb2uart接收到数据事件
#define USB_UART_UART2USB_EVENT  (1<<2) //uart2usb

    typedef struct {
        pDAP_config_t parent;
    // 定义输入数据环形缓冲区句柄，用于存储接收到的uart数据
    RingbufHandle_t uart2usb_RB_handle;
    // 定义输出数据环形缓冲区句柄，用于存储处理后待发送的uart数据
    RingbufHandle_t usb2uart_RB_handle;
    // DAP任务句柄变量，用于通知DAP处理线程进行处理
    TaskHandle_t DapUsbSendTaskHandle;
    TaskHandle_t cdc_acm_TaskHandle;
    EventGroupHandle_t usb_uart_event;
    uint8_t dap_buf[DAP_PACKET_SIZE];//主机用于usb接收数据的缓存
    uint8_t uart_buf[DAP_PACKET_SIZE]; //主机用于usb接收数据的缓存
} DAP_usb_config_t, *pDAP_usb_config_t;

// typedef enum {
//     UART2USB_IDLE,//空闲
//     UART2USB_SEND,//发送中
//     UART2USB_DONE,//上一个发送完成
// }uart2usb_status_t;

extern DAP_usb_config_t g_dap_usb_config;

void dap_usbd_event_rst(void);
void dap_usbd_event_cfg(void);
void dap_usb_init(uint8_t busid, uint32_t reg_base);
void dap_usb_deinit(uint8_t busid);
#endif /*DAP_USB_HANDLE_H */
